<script setup>
import { reactive, onMounted, onBeforeUnmount, ref } from 'vue';
import { getAllSensorData } from '@/api/sensor';
import FarmRecordModal from '@/components/FarmRecordModal.vue';

// 响应式数据
const config = reactive({
  header: ['数据种类', '数值', '单位'],
  data: [],
  index: true,
  columnWidth: [50],
  align: ['center'],
  headerBGC: 'rgba(0, 28, 36, 0.8)',
  oddRowBGC: 'rgba(0, 28, 36, 0.4)',
  evenRowBGC: 'rgba(0, 28, 36, 0.3)',
  waitTime: 3000,
  hoverPause: true,
});

// 弹窗控制
const showModal = ref(false);

// 更新数据
const updateData = async () => {
  try {
    const data = await getAllSensorData();
    config.data = data;
  } catch (error) {
    console.error('更新数据失败:', error);
  }
};

// 组件挂载时开始定时更新
onMounted(() => {
  updateData();
  const timer = setInterval(updateData, 60000);

  onBeforeUnmount(() => {
    clearInterval(timer);
  });
});

const mouseoverHandler = (e) => {
  console.log(e);
};

const clickHandler = (e) => {
  // 点击表格时打开弹窗
  showModal.value = true;
};

// 处理弹窗刷新
const handleRefresh = () => {
  updateData();
};
</script>

<template>
  <div class="right-center">
    <dv-border-box-9 :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']" class="box">
      <div class="content-wrapper">
        <div class="header">
          <div class="title-container">
            <dv-decoration3 class="decoration-left" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']" />
            <dv-border-box-2 class="title-box" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']">
              <div class="title">青枣园农事检测记录</div>
            </dv-border-box-2>
            <dv-decoration3 class="decoration-right" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']" />
          </div>
        </div>

        <div class="table-section" @click="clickHandler">
          <dv-scroll-board :config="config" style="width:460px;height:220px;cursor:pointer;"
            @mouseover="mouseoverHandler" />
          <div class="click-hint">
            <span>点击查看详情</span>
          </div>
        </div>
      </div>
    </dv-border-box-9>

    <!-- 全屏弹窗 -->
    <FarmRecordModal v-model:visible="showModal" :data="config.data" @refresh="handleRefresh" />
  </div>
</template>

<style scoped>
.box {
  width: 485px;
  height: 290px;
  background: rgba(0, 28, 36, 0.3);
}

.right-center {
  width: 100%;
  height: 100%;
}

.content-wrapper {
  width: 100%;
  height: 100%;
  padding: 16px;
  border-radius: 4px;
  background: rgba(0, 28, 36, 0.5);
}

.header {
  width: 100%;
}

.title-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.title-box {
  width: fit-content;
  height: 40px;
  padding: 0 30px;
}

.title {
  color: #00ffd9;
  font-size: 16px;
  font-weight: bold;
  white-space: nowrap;
  padding: 0 10px;
  line-height: 40px;
  text-shadow: 0 0 10px rgba(0, 255, 217, 0.3);
}

.decoration-left,
.decoration-right {
  width: 90px;
  height: 20px;
}

.table-section {
  position: relative;
  cursor: pointer;
}

.click-hint {
  position: absolute;
  bottom: 10px;
  right: 10px;
  color: rgba(0, 255, 217, 0.7);
  font-size: 12px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    opacity: 0.7;
  }

  50% {
    opacity: 1;
  }

  100% {
    opacity: 0.7;
  }
}

:deep(.dv-scroll-board) {
  --header-color: #00ffd9;
  --text-color: #00ffd9;
  --border-color: rgba(0, 255, 217, 0.3);
  --hover-bg: rgba(0, 255, 217, 0.1);
}
</style>